package com.dapeis.api.spring.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ag.core.exception.NoLoginException;
import com.dapeis.api.controller.RestController;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Collocation;
import com.dapeis.exception.api.DataNoSubmitException;
import com.dapeis.exception.api.DataNotAuditException;
import com.dapeis.exception.api.DataWaitAuditException;

/**
 * 搭配师审核状态拦截
 * 
 * @author yuanyong
 *
 */
public class StylistCollocationAuditApilinterceptor implements
	HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
	    HttpServletResponse response, Object handler) throws Exception {
	Collocation collocation = RestController.getThreadLocalCollocation();
	if (collocation == null) {
	    throw new NoLoginException();
	}
	// 判断是否处于已通过状态

	if (collocation.getAuditState() != StylistConstants.Audit.PASS_AUDIT) {
	    if (collocation.getAuditState() == StylistConstants.Audit.NO_SUBMIT) {
		throw new DataNoSubmitException();
	    } else if (collocation.getAuditState() == StylistConstants.Audit.WAIT_AUDIT) {
		// 等待审核
		throw new DataWaitAuditException();
	    } else if (collocation.getAuditState() == StylistConstants.Audit.REJECT_AUDIT) {
		// 判断是否审核未通过。提示审核未通过
		throw new DataNotAuditException();
	    }
	}
	return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
	    HttpServletResponse response, Object handler,
	    ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
	    HttpServletResponse response, Object handler, Exception ex)
	    throws Exception {
    }

}
